﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
using MySql.Data.MySqlClient;
using Summer.IService;
using Summer.Model.Common;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Text;

namespace Summer.Service
{
    public class DBFactory:IDBFactory
    {
        readonly IOptions<DBOptions> _options;

        public DBFactory(IOptions<DBOptions> options)
        {
            _options = options;
        }

        public DbConnection Create()
        {
            DbConnection cnn = null;
            DBOptions dBOptions = _options.Value;
            switch (dBOptions.DbType)
            {
                case DbType.MySQL:
                    cnn = new MySqlConnection(dBOptions.ConnectionString);
                    break;
                case DbType.SQLServer:
                    cnn = new SqlConnection(dBOptions.ConnectionString);
                    break;
                default:
                    throw new Exception("please specify DbType!");
            }
            return cnn;
        }
    }
}
